﻿using MaterialDesignThemes.Wpf;
using Microsoft.Xaml.Behaviors.Core;
using Modules.MSentEvent;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Sorting.App.Core;
using Sorting.App.Views;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Sorting.App.Services;
using Modules.TaskMan.Views;
using Modules.TaskMan.ViewModels;
using LoginView = Sorting.App.Views.LoginView;

namespace Sorting.App.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {


        IEventAggregator ea;

        IContainerExtension _container;
        IRegionManager _regionManager;
        IRegion _region;
        StartLoading startLoadingView;
        private ViewTaskMainV2ViewModel _taskManViewModel;
        private ShelfService _shelfService;
        ViewTaskMainV2 viewTaskMan;

        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public DelegateCommand LoadDataCommand { get; private set; }
        public MainWindowViewModel(
            IContainerExtension Container,
            IRegionManager regionManager,
            IEventAggregator eventAggregator,
            ShelfService shelfService,
            ViewTaskMainV2ViewModel taskManViewModel)
        {
            #region 接受登陆消息
            ea = eventAggregator;
            ea.GetEvent<LoadingSentEvent>().Subscribe(MessageReceived);
            #endregion

            _regionManager = regionManager;
            _container = Container;
            _shelfService = shelfService;
            _taskManViewModel = taskManViewModel;

            LoadDataCommand = new DelegateCommand(LoadDataAsync);
        }

        /// <summary>
        /// 接收消息   跳转主页面
        /// </summary>
        /// <param name="loginState"></param>
        private async void MessageReceived(int  loginState)
        {

            if (loginState==0)
            {
                //初始主界面的储位数据
                var entitys = await _shelfService.GetShelves();
                _taskManViewModel.ShelfEntities = entitys.ToList();
                _taskManViewModel.ShelfEntitiesA = _taskManViewModel.ShelfEntities.Where(t => t.LeftOrRight.Equals("L")).ToList();
                _taskManViewModel.ShelfEntitiesB = _taskManViewModel.ShelfEntities.Where(t => t.LeftOrRight.Equals("R")).ToList();
                
                _region.Deactivate(startLoadingView);
              
                viewTaskMan = _container.Resolve<ViewTaskMainV2>();
                viewTaskMan.DataContext = _taskManViewModel;
                _region.Add(viewTaskMan);
                Title = "主界面";
                ea.GetEvent<LoadingSentEvent>().Publish(1);
                //弹出登录窗口
                var view = _container.Resolve<LoginView>();
                var result = await DialogHost.Show(view, "RootDialog");
            }

        }


        /// <summary>
        /// 页面加载时处理
        /// </summary>\
        /// 
        private  void LoadDataAsync()
        {

            _region = _regionManager.Regions[RegionNames.ContentRegion];
            startLoadingView = _container.Resolve<StartLoading>();

            Title = "初始化";
            _region.Add(startLoadingView);
        }


    }
}
